/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "seccamconservos.h"
#include <iostream>

SecCamConServos::SecCamConServos(QWidget *parent)
 : QWidget(parent)
{
	this->setAutoFillBackground(true);
	ui.setupUi(this);
  QPixmap bgpixmap = QPixmap(":images/modules/camcon/servos/bkg_servos.png");
  if(bgpixmap.isNull()) std::cout<<"SecCamConServos: null background pixmap"<<std::endl;
  QPalette p = this->palette();
  p.setBrush(this->backgroundRole(),QBrush(bgpixmap));
  this->setPalette(p);
  ui.btnPower->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_power_bus_enable_inactive.png"));
  ui.btnPower->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_power_bus_enable_pressed.png"));
  ui.btnPower->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_power_bus_enable_disabled.png"));
  ui.btnOpenAll->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_open_all_inactive.png"));
  ui.btnOpenAll->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_open_all_pressed.png"));
  ui.btnOpenAll->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_open_all_disabled.png"));
  ui.btnOpenAll->setEnabled(false);
  ui.btnCloseAll->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_close_all_inactive.png"));
  ui.btnCloseAll->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_close_all_pressed.png"));
  ui.btnCloseAll->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_close_all_disabled.png"));
  ui.btnCloseAll->setEnabled(false);
  ui.btnServo1->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_servo1_inactive.png"));
  ui.btnServo1->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_servo1_pressed.png"));
  ui.btnServo1->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_servo1_disabled.png"));
  ui.btnServo1->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/servos/btn_servo1_active.png"));
  ui.btnServo1->setEnabled(false);
  ui.btnServo2->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_servo2_inactive.png"));
  ui.btnServo2->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_servo2_pressed.png"));
  ui.btnServo2->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_servo2_disabled.png"));
  ui.btnServo2->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/servos/btn_servo2_active.png"));
  ui.btnServo2->setEnabled(false);
  ui.btnServo3->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_servo3_inactive.png"));
  ui.btnServo3->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_servo3_pressed.png"));
  ui.btnServo3->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_servo3_disabled.png"));
  ui.btnServo3->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/servos/btn_servo3_active.png"));
  ui.btnServo3->setEnabled(false);
  ui.btnRotLeftFast->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_rotate_left_fast_inactive.png"));
  ui.btnRotLeftFast->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_rotate_left_fast_pressed.png"));
  ui.btnRotLeftFast->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_rotate_left_fast_disabled.png"));
  ui.btnRotLeftFast->setEnabled(false);
  ui.btnRotLeft->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_rotate_left_slow_inactive.png"));
  ui.btnRotLeft->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_rotate_left_slow_pressed.png"));
  ui.btnRotLeft->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_rotate_left_slow_disabled.png"));
  ui.btnRotLeft->setEnabled(false);
  ui.btnRotRightFast->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_rotate_right_fast_inactive.png"));
  ui.btnRotRightFast->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_rotate_right_fast_pressed.png"));
  ui.btnRotRightFast->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_rotate_left_right_disabled.png"));
  ui.btnRotRightFast->setEnabled(false);
  ui.btnRotRight->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_rotate_right_slow_inactive.png"));
  ui.btnRotRight->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_rotate_right_slow_pressed.png"));
  ui.btnRotRight->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_rotate_right_slow_disabled.png"));
  ui.btnRotRight->setEnabled(false);
  ui.btnSaveOpen->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_save_as_open_inactive.png"));
  ui.btnSaveOpen->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_save_as_open_pressed.png"));
  ui.btnSaveOpen->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_save_as_open_disabled.png"));
  ui.btnSaveOpen->setEnabled(false);
  ui.btnSaveClosed->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_save_as_closed_inactive.png"));
  ui.btnSaveClosed->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_save_as_closed_pressed.png"));
  ui.btnSaveClosed->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_save_as_closed_disabled.png"));
  ui.btnSaveClosed->setEnabled(false);
  ui.btnOpenSelected->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_open_selected_inactive.png"));
  ui.btnOpenSelected->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_open_selected_pressed.png"));
  ui.btnOpenSelected->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_open_selected_disabled.png"));
  ui.btnOpenSelected->setEnabled(false);
  ui.btnCloseSelected->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_close_selected_inactive.png"));
  ui.btnCloseSelected->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_close_selected_pressed.png"));
  ui.btnCloseSelected->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_close_selected_disabled.png"));
  ui.btnCloseSelected->setEnabled(false);
  ui.btnPowerDisable->replaceStatePixmap(PStyledButton::off,QPixmap(":images/modules/camcon/servos/btn_power_bus_disable_inactive.png"));
  ui.btnPowerDisable->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_power_bus_disable_pressed.png"));
  ui.btnPowerDisable->replaceStatePixmap(PStyledButton::disabled,QPixmap(":images/modules/camcon/servos/btn_power_bus_disable_disabled.png"));
  ui.btnPowerDisable->setEnabled(false);
  connect(ui.btnPower,SIGNAL(clicked()),this,SLOT(powerBusEnable()));
  connect(ui.btnOpenAll,SIGNAL(clicked()),this,SLOT(openAll()));
  connect(ui.btnCloseAll,SIGNAL(clicked()),this,SLOT(closeAll()));
  connect(ui.btnServo1,SIGNAL(clicked()),this,SLOT(servo1()));
  connect(ui.btnServo2,SIGNAL(clicked()),this,SLOT(servo2()));
  connect(ui.btnServo3,SIGNAL(clicked()),this,SLOT(servo3()));
  connect(ui.btnRotLeftFast,SIGNAL(clicked()),this,SLOT(rotateLeftFast()));
  connect(ui.btnRotLeft,SIGNAL(clicked()),this,SLOT(rotateLeftSlow()));
  connect(ui.btnRotRightFast,SIGNAL(clicked()),this,SLOT(rotateRightFast()));
  connect(ui.btnRotRight,SIGNAL(clicked()),this,SLOT(rotateRightSlow()));
  connect(ui.btnSaveOpen,SIGNAL(clicked()),this,SLOT(saveAsOpen()));
  connect(ui.btnSaveClosed,SIGNAL(clicked()),this,SLOT(saveAsClosed()));
  connect(ui.btnOpenSelected,SIGNAL(clicked()),this,SLOT(openSelected()));
  connect(ui.btnCloseSelected,SIGNAL(clicked()),this,SLOT(closeSelected()));
  connect(ui.btnPowerDisable,SIGNAL(clicked()),this,SLOT(powerBusDisable()));
  connect(ui.btnExit,SIGNAL(clicked()),this,SLOT(exit()));
}


SecCamConServos::~SecCamConServos()
{
}




/*!
    \fn SecCamConServos::exit()
 */
void SecCamConServos::exit()
{
    emit sig_exit();
}


/*!
    \fn SecCamConServos::closeAll()
 */
void SecCamConServos::closeAll()
{
    emit sig_closeAll();
}


/*!
    \fn SecCamConServos::closeSelected()
 */
void SecCamConServos::closeSelected()
{
    emit sig_closeSelected();
}


/*!
    \fn SecCamConServos::openAll()
 */
void SecCamConServos::openAll()
{
    emit sig_openAll();
}


/*!
    \fn SecCamConServos::openSelected()
 */
void SecCamConServos::openSelected()
{
    emit sig_openSelected();
}


/*!
    \fn SecCamConServos::powerBusDisable()
 */
void SecCamConServos::powerBusDisable()
{
    ui.btnPower->setEnabled(true);
    ui.btnOpenAll->setEnabled(false);
    ui.btnCloseAll->setEnabled(false);
    ui.btnServo1->setEnabled(false);
    ui.btnServo2->setEnabled(false);
    ui.btnServo3->setEnabled(false);
    ui.btnRotLeftFast->setEnabled(false);
    ui.btnRotLeft->setEnabled(false);
    ui.btnRotRightFast->setEnabled(false);
    ui.btnRotRight->setEnabled(false);
    ui.btnSaveOpen->setEnabled(false);
    ui.btnSaveClosed->setEnabled(false);
    ui.btnOpenSelected->setEnabled(false);
    ui.btnCloseSelected->setEnabled(false);
    ui.btnPowerDisable->setEnabled(false);
    //ui.btnExit->setEnabled(false);
    emit sig_powerBusEnable();
}


/*!
    \fn SecCamConServos::powerBusEnable()
 */
void SecCamConServos::powerBusEnable()
{
    ui.btnOpenAll->setEnabled(true);
    ui.btnCloseAll->setEnabled(true);
    ui.btnServo1->setEnabled(true);
    ui.btnServo2->setEnabled(true);
    ui.btnServo3->setEnabled(true);
//     ui.btnRotLeftFast->setEnabled(true);
//     ui.btnRotLeft->setEnabled(true);
//     ui.btnRotRightFast->setEnabled(true);
//     ui.btnRotRight->setEnabled(true);
    ui.btnSaveOpen->setEnabled(true);
    ui.btnSaveClosed->setEnabled(true);
    ui.btnOpenSelected->setEnabled(true);
    ui.btnCloseSelected->setEnabled(true);
    ui.btnPowerDisable->setEnabled(true);
    ui.btnExit->setEnabled(true);
    ui.btnPower->setEnabled(false);
    emit sig_powerBusDisable();
}


/*!
    \fn SecCamConServos::rotateLeftFast()
 */
void SecCamConServos::rotateLeftFast()
{
    emit sig_rotateLeftFast();
}


/*!
    \fn SecCamConServos::rotateLeftSlow()
 */
void SecCamConServos::rotateLeftSlow()
{
    emit sig_rotateLeftSlow();
}


/*!
    \fn SecCamConServos::rotateRightFast()
 */
void SecCamConServos::rotateRightFast()
{
    emit sig_rotateRightFast();
}


/*!
    \fn SecCamConServos::rotateRightSlow()
 */
void SecCamConServos::rotateRightSlow()
{
    emit sig_rotateRightSlow();
}


/*!
    \fn SecCamConServos::saveAsClosed()
 */
void SecCamConServos::saveAsClosed()
{
    emit sig_saveAsClosed();
}


/*!
    \fn SecCamConServos::saveAsOpen()
 */
void SecCamConServos::saveAsOpen()
{
   emit sig_saveAsOpen();
}


/*!
    \fn SecCamConServos::servo1()
 */
void SecCamConServos::servo1()
{
    ui.btnServo1->setChecked(true);
    ui.btnServo2->setChecked(false);
    ui.btnServo3->setChecked(false);
    ui.btnServo1->setState(PStyledButton::on);
    ui.btnServo2->setState(PStyledButton::off);
    ui.btnServo3->setState(PStyledButton::off);
    ui.btnRotLeftFast->setEnabled(true);
    ui.btnRotLeft->setEnabled(true);
    ui.btnRotRightFast->setEnabled(true);
    ui.btnRotRight->setEnabled(true);
    emit sig_servo1();
}


/*!
    \fn SecCamConServos::servo2()
 */
void SecCamConServos::servo2()
{
    ui.btnServo2->setChecked(true);
    ui.btnServo3->setChecked(false);
    ui.btnServo1->setChecked(false);
    ui.btnServo2->setState(PStyledButton::on);
    ui.btnServo1->setState(PStyledButton::off);
    ui.btnServo3->setState(PStyledButton::off);
    ui.btnRotLeftFast->setEnabled(true);
    ui.btnRotLeft->setEnabled(true);
    ui.btnRotRightFast->setEnabled(true);
    ui.btnRotRight->setEnabled(true);
    emit sig_servo2();
}


/*!
    \fn SecCamConServos::servo3()
 */
void SecCamConServos::servo3()
{
    ui.btnServo3->setChecked(true);
    ui.btnServo2->setChecked(false);
    ui.btnServo1->setChecked(false);
    ui.btnServo3->setState(PStyledButton::on);
    ui.btnServo2->setState(PStyledButton::off);
    ui.btnServo1->setState(PStyledButton::off);
    ui.btnRotLeftFast->setEnabled(true);
    ui.btnRotLeft->setEnabled(true);
    ui.btnRotRightFast->setEnabled(true);
    ui.btnRotRight->setEnabled(true);
    emit sig_servo3();
}



